<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="isAllchecked"   /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in goodList" :key="item.name">
          <td>
            <input
              type="checkbox"
              :checked="item.checked"
              @input="handleInput(item.name, $event)"
            />
          </td>
          <td>{{ item.name }}</td>
          <td>{{ item.price }}</td>
          <td>
            <button @click="addFn(-1, item.name, item.num)">-</button
            >{{ item.num }}
            <button @click="addFn(+1, item.name)">+</button>
          </td>
          <td>{{ item.num * item.price }}</td>
          <td><button @click="delItem(item.name)">删除</button></td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">{{itemTal}}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
  created() {},
  computed: {
    ...mapState(["goodList"]),
    ...mapGetters(['isshowAll','itemTal']),
    /* 当true的长度 等于总长度的时候 返回true 用：checked绑定 */
      isAllchecked:{
    get(){
      return this.goodList.length ===  this.isshowAll
    },
    set(val){
  this.$store.commit('itemShow',val)
    }
    }
  },
  data() {
    return {};
  },
  methods: {
    addFn(num, name, count) {
      if (count < 1) return;
      this.$store.commit("addFn", {
        num,
        name,
      });
    },
    delItem(name) {
      this.$store.commit("delgoodList", name);
    },
    handleInput(name, e) {
      this.$store.commit("isshow", {
        name,
        e: e.target.value,
      });
    },
   updateChecked(){
    this.itemTal =  !this.itemTal
   }
  },

};
</script>

<style></style>
